// SDP_1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <string>
#include <vector>

using namespace std;

// SDP媒体类型
enum SdpMediaType {
	SDP_MEDIA_TYPE_AUDIO,
	SDP_MEDIA_TYPE_VIDEO,
	SDP_MEDIA_TYPE_DATA
};

// SDP媒体格式
struct SdpMediaFormat {
	string payload_type;        // 负载类型
	string encoding_name;       // 编码名称
	string clock_rate;          // 时钟频率
	string channels;            // 声道数
	vector<string> attributes;  // 属性列表
};

// SDP媒体信息
struct SdpMediaInfo {
	SdpMediaType type;                  // 媒体类型
	string port;                        // 端口号
	string protocol;                    // 传输协议
	vector<SdpMediaFormat> formats;     // 支持的格式列表
	vector<string> attributes;          // 属性列表
};

// SDP会话信息
struct SdpSessionInfo {
	string version;                     // SDP版本
	string origin_username;             // 会话发起者用户名
	string origin_session_id;           // 会话ID
	string origin_session_version;      // 会话版本号
	string origin_network_type;         // 网络类型
	string origin_address_type;         // 地址类型
	string origin_address;              // 地址
	string session_name;                // 会话名称
	vector<SdpMediaInfo> media_infos;   // 媒体信息列表
};

// 创建SDP会话信息
string create_sdp_session(const SdpSessionInfo& session_info)
{
	string sdp;

	// 添加版本信息
	sdp += "v=" + session_info.version + "\r\n";

	// 添加会话发起者信息
	sdp += "o=" + session_info.origin_username + " " + session_info.origin_session_id + " " +
		session_info.origin_session_version + " " + session_info.origin_network_type + " " +
		session_info.origin_address_type + " " + session_info.origin_address + "\r\n";

	// 添加会话名称
	sdp += "s=" + session_info.session_name + "\r\n";

	// 添加媒体信息
	for (const auto& media_info : session_info.media_infos) {
		// 添加媒体类型
		switch (media_info.type) {
		case SDP_MEDIA_TYPE_AUDIO:
			sdp += "m=audio " + media_info.port + " " + media_info.protocol + " ";
			break;
		case SDP_MEDIA_TYPE_VIDEO:
			sdp += "m=video " + media_info.port + " " + media_info.protocol + " ";
			break;
		case SDP_MEDIA_TYPE_DATA:
			sdp += "m=data " + media_info.port + " " + media_info.protocol + " ";
			break;
		}

		// 添加媒体格式
		for (const auto& media_format : media_info.formats) {
			sdp += media_format.payload_type + " " + media_format.encoding_name + " " +
				media_format.clock_rate + " " + media_format.channels;
			for (const auto& attribute : media_format.attributes) {
				sdp += " " + attribute;
			}
			sdp += "\r\n";
		}

		// 添加媒体属性
		for (const auto & attribute : media_info.attributes) {
			sdp += "a=" + attribute + "\r\n";
		}
	}

	return sdp;
}

int main()
{
	SdpSessionInfo session_info;
	session_info.version = "0";
	session_info.origin_username = "-";
	session_info.origin_session_id = "1";
	session_info.origin_session_version = "1";
	session_info.origin_network_type = "IN";
	session_info.origin_address_type = "IP4";
	session_info.origin_address = "127.0.0.1";
	session_info.session_name = "Test Session";

	SdpMediaInfo audio_info;
	audio_info.type = SDP_MEDIA_TYPE_AUDIO;
	audio_info.port = "7070";
	audio_info.protocol = "RTP/AVP";
	SdpMediaFormat pcmu_format;
	pcmu_format.payload_type = "0";
	pcmu_format.encoding_name = "PCMU";
	pcmu_format.clock_rate = "8000";
	pcmu_format.channels = "1";
	pcmu_format.attributes = { "rtpmap:0 PCMU/8000" };
	audio_info.formats.push_back(pcmu_format);
	audio_info.attributes = { "sendrecv" };
	session_info.media_infos.push_back(audio_info);

	string sdp = create_sdp_session(session_info);
	cout << sdp << endl;

	return 0;

}